package application;

import javax.swing.SwingUtilities;

import network.IpTableEntry;
import network.NetworkServerClientRequestHandler;

import persistence.PersistenceManager;
import requestManager.EmailApplicationRequestManager;
import resources.ResourceProvider;

import model.EmailApplicationModel;

import view.EmailApplicationView;

/**
 * Main Email Application Manager
 */
public class EmailApplicationManager 
{

	private EmailApplicationView emailAppView;
	private EmailApplicationRequestManager emailAppController;
	private EmailApplicationModel emailAppModel;
	private PersistenceManager persistenceManager;
	private NetworkServerClientRequestHandler networkRequestHandler;
	
    /**
	 * The email client application is launched from this class containing the 'main' method.
	 * @param args arguments
	 */
	public static void main(String[] args) 
	{
		SwingUtilities.invokeLater(new Runnable() 
		{
			/*
			 * (non-Javadoc)
			 * @see java.lang.Runnable#run()
			 */
			public void run() 
			{
                EmailApplicationManager app = new EmailApplicationManager();
                app.runEmailApplication();
			}
		});
	}

    /**
	 * to run Email Application, create Model,Controller,View and Persistence components
	 */
	public void runEmailApplication() 
	{   
         networkRequestHandler = new NetworkServerClientRequestHandler();
         networkRequestHandler.updateWith(new IpTableEntry(ResourceProvider.SERVER, NetworkServerClientRequestHandler.ip()));
	
 
        emailAppView = new EmailApplicationView();
	    emailAppController = new EmailApplicationRequestManager(emailAppView,networkRequestHandler);
	    networkRequestHandler.setAppRequestHandler(emailAppController);
	    emailAppModel = new EmailApplicationModel();
	    persistenceManager = new PersistenceManager();
	    
	    
		// display login
	    emailAppView.displayLogin();
		
		emailAppView.registerController(emailAppController);
		emailAppController.registerModel(emailAppModel);
		emailAppModel.registerPersistenceManager(persistenceManager);
	}
	
	/**
	 * Exit application
	 */
	public void reset()
	{
	    emailAppView.unRegisterController();
        emailAppController.unRegiserModel();
        emailAppModel.unRegisterPersistenceManager();
	    emailAppView = null;
	    emailAppController = null;
	    emailAppModel = null;
	    persistenceManager = null;
	}

}
